package com.du.web; /**
 * @author : DuJiabao
 * @Date : 2021/2/1 23:18
 * @Project : bookstore
 * @File : ${FILE_NAME}
 * @Desc :
 */

import com.du.pojo.Book;
import com.du.service.BookService;
import com.du.service.impl.BookServiceImpl;
import com.du.utils.WebUtils;

import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.annotation.*;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.List;

@WebServlet(name = "ManagerServlet", value = "/managerServlet")
public class ManagerServlet extends BaseServlet {
    BookService bookService = new BookServiceImpl();

    /**
     * 处理图书查询请求，并返回图书列表
     *
     * @param request
     * @param response
     * @throws ServletException
     * @throws IOException
     */
    private void list(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        List<Book> books = bookService.bookList();
        request.setAttribute("books", books);
        request.getRequestDispatcher("pages/manager/book_manager.jsp").forward(request, response);
    }

    /**
     * 处理修改图书的请求，并请求转发到修改页面
     *
     * @param request
     * @param response
     * @throws ServletException
     * @throws IOException
     */
    private void display(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String idString = request.getParameter("id");
        int id = Integer.parseInt(idString);
        Book book = bookService.getBook(id);
        request.setAttribute("book", book);
        request.getRequestDispatcher("pages/manager/book_edit.jsp?action=update").forward(request, response); // 请求转发到图书编辑
    }

    /**
     * 处理修改图书内容的请求，并重定向回管理页面
     *
     * @param request
     * @param response
     * @throws ServletException
     * @throws IOException
     */
    private void update(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        Book book = WebUtils.copyParamToBean(request.getParameterMap(), new Book());
        System.out.println(book);
        bookService.updateBook(book);
        response.sendRedirect("http://localhost:8080/bookstore/managerServlet?action=list");  // 重定向回图书管理页面
    }

    /**
     * 处理图书删除请求，并重定向到管理页面
     *
     * @param request
     * @param response
     * @throws ServletException
     * @throws IOException
     */
    private void delete(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String idStr = request.getParameter("id");
        int id = Integer.parseInt(idStr);
        bookService.deleteBook(id);
        response.sendRedirect("http://localhost:8080/bookstore/managerServlet?action=list");  // 重定向回图书管理页面
    }

    /**
     * 处理图书添加请求，并重定向到管理页面
     *
     * @param request
     * @param response
     * @throws ServletException
     * @throws IOException
     */
    private void add(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        Book book = WebUtils.copyParamToBean(request.getParameterMap(), new Book());
        bookService.addBook(book);
        response.sendRedirect("http://localhost:8080/bookstore/managerServlet?action=list"); // 重定向到图书管理页面
    }
}
